package agents;

import java.io.IOException;
import java.util.List;

import behaviours.RegisterServiceBehaviour;

import com.myprofile.profile.MuseumItem;
import com.myprofile.profile.MuseumItemDTO;
import model.Inventory;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;
import jade.domain.FIPANames;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

public class InventoryAgent extends Agent {

	private static final long serialVersionUID = -419441554554021315L;
	List<MuseumItem> items;

	@Override
	protected void setup() {

		System.out.println(this.getName()
				+ ": Setting up as an Inventory Agent...");
		Inventory inv = new Inventory();
		items = inv.getItems();
		
		// Register with DF
		registerWithDF();
		
		System.out.println(this.getName() + ": Listening for item requests...");
		this.addBehaviour(new ListenForItemRequestBehaviour());
	}
	
	private void registerWithDF() {
		ServiceDescription sd = new ServiceDescription();
		sd.setType("inventory-lookup");
		sd.setName("inventory");
		ServiceDescription[] sds = new ServiceDescription[1];
		sds[0] = sd;
		addBehaviour(new RegisterServiceBehaviour(getAID(), sds));
	}

	private class ListenForItemRequestBehaviour extends CyclicBehaviour {

		@Override
		public void action() {

			ACLMessage msg = myAgent.receive(MessageTemplate
					.MatchProtocol(FIPANames.InteractionProtocol.FIPA_QUERY));

			if (msg != null) {
				if(msg.getPerformative() == ACLMessage.REQUEST) {
					System.out.println(myAgent.getName()
							+ ": Item request received!");
					myAgent.addBehaviour(new SendRequestedItemBehaviour(msg));
				}
			} else {
				block();
			}

		}
	}

	private class SendRequestedItemBehaviour extends OneShotBehaviour {

		private ACLMessage request;

		public SendRequestedItemBehaviour(ACLMessage request) {
			this.request = request;
		}

		@Override
		public void action() {
			// What was the id for the item requested
			String requestedItemId = request.getContent();

			MuseumItemDTO matchingItem = null;

			for (MuseumItem item : items) {
				if (requestedItemId.equals(item.getId()))
					matchingItem = item;
			}
			
			try {
				ACLMessage reply = request.createReply();
				
				if (matchingItem == null) {
					System.out.println(myAgent.getName()
							+ ": No matching item found. ");
					reply.setPerformative(ACLMessage.REFUSE);
					reply.setContent(requestedItemId);
				} else {
					System.out.println(myAgent.getName()
							+ ": Matching item found!");
					
					reply.setPerformative(ACLMessage.INFORM);
					reply.setContentObject(matchingItem);
				}
				
				System.out.println(myAgent.getName() + ": Sending reply to requester: "
							+ request.getSender().getName());
				myAgent.send(reply);
			} 
			// Shouldnt happen
			catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
